package com.example.demo.config;

import com.example.demo.mvc.frame.GPServletDispather;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

/**
 * @author zzf
 * @date 2018/8/10 23:08.
 */
@Configuration
public class ServletConfig {

    @Bean
    public ServletRegistrationBean reportServlet(){
        //注解扫描上下文
        AnnotationConfigWebApplicationContext applicationContext
                = new AnnotationConfigWebApplicationContext();
        //base package
        applicationContext.scan("com.example.demo");
        //通过构造函数指定dispatcherServlet的上下文
//        DispatcherServlet rest_dispatcherServlet
//                = new DispatcherServlet(applicationContext);
        GPServletDispather rest_dispatcherServlet = new GPServletDispather();

        //用ServletRegistrationBean包装servlet
        ServletRegistrationBean registrationBean
                = new ServletRegistrationBean(rest_dispatcherServlet);
//        registrationBean.addInitParameter("", "");
        registrationBean.setLoadOnStartup(1);
        //指定urlmapping
        registrationBean.addUrlMappings("/zzf/*");
        //指定name，如果不指定默认为dispatcherServlet
        registrationBean.setName("demo");
        return registrationBean;
    }

}
